package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//@CrossOrigin
@RestController
@RequestMapping("/admin/product")
public class ManageController {

    @Resource
    private ManageService manageService;

    //获取一级分类数据
    @GetMapping("/getCategory1")
    public Result getCategory1(){

        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    //获取二级分类数据
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){

        List<BaseCategory2> baseCategory2ListList = manageService.getCategory2(category1Id);
        return Result.ok(baseCategory2ListList);
    }

    //获取三级分类数据
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){

        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //根据分类id获取平台属性
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id){
        List<BaseAttrInfo> baseAttrInfoList = manageService.attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    //添加平台属性
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        Long id = baseAttrInfo.getId();
        if (id != null){
            manageService.updateAttrInfo(baseAttrInfo);
        } else {
            manageService.saveAttrInfo(baseAttrInfo);
        }
        return Result.ok();
    }

    //获取品牌分页列表
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Integer page,
            @PathVariable Integer limit
    ){
        IPage iPage = manageService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }

    //根据平台属性ID获取平台属性
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
        return Result.ok(baseAttrValueList);
    }

    //获取spu分页列表
    @GetMapping("/{page}/{limit}")
    public Result findSpuList(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            Long category3Id){
        IPage iPage = manageService.findSpuList(page,limit,category3Id);
        return Result.ok(iPage);
    }

    //获取品牌属性列表
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //品牌列表添加
    @PostMapping("/baseTrademark/save")
    public Result baseTrademarkSave(@RequestBody BaseTrademark baseTrademark){
        manageService.baseTrademarkSave(baseTrademark);
        return  Result.ok();
    }

    //品牌列表删除
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result baseTrademarkRemove(@PathVariable Long id){
        manageService.baseTrademarkRemove(id);
        return Result.ok();
    }

    //查询品牌列表某个属性
    @GetMapping("/baseTrademark/get/{id}")
    public Result baseTrademarkGet(@PathVariable Long id){
        BaseTrademark baseTrademark = manageService.baseTrademarkGet(id);
        return Result.ok(baseTrademark);
    }

    //修改品牌列表某个属性
    @PutMapping("/baseTrademark/update")
    public Result baseTrademarkUpdate(@RequestBody BaseTrademark baseTrademark){
        manageService.baseTrademarkUpdate(baseTrademark);
        return Result.ok();
    }

    //获取销售属性
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //添加spu
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //根据spuId获取图片列表
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage> spuImageList = manageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //根据spuId获取销售属性及属性值
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //添加sku
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //获取sku分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result skuList(@PathVariable Integer page,
                          @PathVariable Integer limit){

        IPage iPage = manageService.skuList(page,limit);
        return Result.ok(iPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }
}
